package com.zznj.xzjc.adapter.index;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.view.dialog.RxDialogDownload;
import com.zznj.xzjc.R;
import com.zznj.xzjc.activity.FileDisplayActivity;
import com.zznj.xzjc.activity.NetPicShowActivity;
import com.zznj.xzjc.bean.FuJianBean;
import com.zznj.xzjc.bean.index.FileData;
import com.zznj.xzjc.net.rxjava.HttpMethods;
import com.zznj.xzjc.utils.FileUtils;
import com.zznj.xzjc.utils.fileutils.OkManager;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述：添加类的描述
 *
 * @author wx5e48d604c2885118
 * @time 2020/4/7
 */
public class FileDataAdapter extends BaseQuickAdapter<FileData, BaseViewHolder> {

    public FileDataAdapter(int layoutResId, @Nullable List<FileData> data) {
        super(layoutResId, data);
    }

    private int type;

    public FileDataAdapter(int layoutResId, @Nullable List<FileData> data, int type) {
        super(layoutResId, data);
        this.type = type;
    }

    @Override
    protected void convert(BaseViewHolder helper, FileData item) {
        helper.setText(R.id.fileName, item.getFileName());
        ImageView image = helper.getView(R.id.fileIcon);
        image.setImageResource(FileUtils.getIcon(item.getFileName()));
        helper.getView(R.id.fileLinear).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("xxxxx", "32232332");
                    if ("jpg,jpeg,png,gif".contains(item.getFileName().substring(item.getFileName().lastIndexOf(".") + 1))) {
                        List<String> pList = new ArrayList<>();
                        pList.add(HttpMethods.BASE_File_URL + item.getFileId());
                        Bundle bundle = new Bundle();
                        bundle.putInt("item", 0);
                        bundle.putSerializable("listPath", (Serializable) pList);
                        RxActivityTool.skipActivity(mContext, NetPicShowActivity.class, bundle);
                    } else {
                        downFile(HttpMethods.BASE_File_URL + item.getFileId(), item.getFileName());
                    }

            }
        });

    }

    private void downFile(String downUrl, String fileName) {
        File pFile = new File(FileUtils.getRootFile(), fileName);
        if (!pFile.exists()) {
            initDownDialog();
            OkManager.getInstance().downLoadFile(downUrl, fileName, new Handler() {
                @Override
                public void handleMessage(@NonNull Message msg) {
                    downDialog.dismiss();
                    if (msg.what == 100) {
                        RxToast.normal("下载成功！");
                        Bundle bundle = new Bundle();
                        bundle.putString("fileName", fileName);
                        RxActivityTool.skipActivity(mContext, FileDisplayActivity.class, bundle);
                    } else if (msg.what == -1) {
                        RxToast.normal("下载失败！");
                        pFile.delete();
                    }
                    super.handleMessage(msg);
                }
            });
        } else {
            Bundle bundle = new Bundle();
            bundle.putString("fileName", fileName);
            RxActivityTool.skipActivity(mContext, FileDisplayActivity.class, bundle);
        }
    }

    private RxDialogDownload downDialog;

    private void initDownDialog() {
        downDialog = new RxDialogDownload(mContext);
        downDialog.setCanceledOnTouchOutside(false);
        downDialog.getTextView().setText("下载中...");
        downDialog.show();
    }
}
